package com.decades.application.main;

import java.util.List;

import com.aphidmobile.flip.FlipViewController;
import com.decades.application.adapter.KmsMenuListAdapter;
import com.decades.application.dto.BlogDto;
import com.decades.application.dto.SlideDto;
import com.decades.application.model.Menu;
import com.jakewharton.salvage.ProgressHUD;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.Toast;

@SuppressLint("NewApi") public class KMSMenuActivity extends Activity {
	private final Handler handler = new Handler();
	private FlipViewController flipView;
	private ListView kmsMenuList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//		setContentView(R.layout.activity_kms_menu);
//		final int color = Color.parseColor("#9166ad");
//		new ScreenSizeUntil(getApplicationContext(), this, RightMenuActivity.class).changeColor(color,getIntent().getStringExtra("ActionBarTitle"));
//		
//		kmsMenuList = (ListView) findViewById(R.id.kmsmenuList);
//		
//		new FetchKMSMenu().execute();
	    System.gc();
		flipView = new FlipViewController(this);
		flipView.setAnimationBitmapFormat(Bitmap.Config.RGB_565);
		new InitFlipView().execute();		
		setContentView(flipView);
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
//		overridePendingTransition(R.anim.open_main, R.anim.close_next);
		back(null);
	}
	public void back(View v) {
		Intent i = null ;
		if(getIntent().getIntExtra("StateFrom", 0)==0){
			i= new Intent(this,RightMenuActivity.class);
		}else{
			i= new Intent(this,NrctMainActivity.class);
		}
		startActivity(i);
		this.finish();
	}
	private Drawable.Callback drawableCallback = new Drawable.Callback() {
		@Override
		public void invalidateDrawable(Drawable who) {
			getActionBar().setBackgroundDrawable(who);
		}

		@Override
		public void scheduleDrawable(Drawable who, Runnable what, long when) {
			handler.postAtTime(what, when);
		}

		@Override
		public void unscheduleDrawable(Drawable who, Runnable what) {
			handler.removeCallbacks(what);
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		flipView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		flipView.onPause();
	}
	public class InitFlipView extends AsyncTask<String, Void, List<Menu>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		List<SlideDto> list;
		List<BlogDto> imgList;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(KMSMenuActivity.this,"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<Menu> doInBackground(String... params) {
			// TODO Auto-generated method stub
			//JSONParserForGetList json =  JSONParserForGetList.getInstance();
			Menu menu = new Menu();
			
			List<Menu> menuList = menu.getMenuSelected();
//			if(menuList==null || menuList.isEmpty()){
//				User user = new User();
//				String token = "";
//				if(user.getUserLogin()!=null){
//					
//					token = user.getUserLogin().token;
//				}
//				JSONParserForGetList.getInstance().getMenus(token);
//				menuList = menu.getMenuSelected();
//			}
//			list = JSONParserForGetList.getInstance().getSlides();
//			imgList = JSONParserForGetList.getInstance().getImageFiftySlide();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return menuList;
		}
		
		@Override
		protected void onPostExecute(List<Menu> result) {
			super.onPostExecute(result);
			if (result != null) {
				flipView.setAdapter(new KmsMenuListAdapter(KMSMenuActivity.this,result,list,imgList));
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
				(Toast.makeText(getApplicationContext(), "An error occured.", Toast.LENGTH_LONG)).show();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}

	}
//	public class FetchKMSMenu extends AsyncTask<String, Void, List<MenuDto>> implements OnCancelListener{
//		ProgressHUD mProgressHUD;
//		
//    	@Override
//    	protected void onPreExecute() {
//        	mProgressHUD = ProgressHUD.show(KMSMenuActivity.this,"Loading Content...", true,true,this);
//    		super.onPreExecute();
//    	}
//		@Override
//		protected List<MenuDto> doInBackground(String... params) {
//			// TODO Auto-generated method stub
//			JSONParserForGetList json =  JSONParserForGetList.getInstance();
//			String token = "";
//			User user = new User();
//			if(user.getUserLogin()!=null){
//				
//				token = user.getUserLogin().token;
//			}
//			
//			List<MenuDto> menuList = json.getMenus(token);
////			MenuDto menu = new MenuDto();
////			menu.setId("999");
////			menu.setName(getResources().getString(R.string.professor_menu));
////			menuList.add(menu);
//			return menuList;
//		}
//		
//		@Override
//		protected void onPostExecute(List<MenuDto> result) {
//			super.onPostExecute(result);
//			if (result != null) {
//				KmsMenuListAdapter jmla = new KmsMenuListAdapter(KMSMenuActivity.this, result);
//				kmsMenuList.setAdapter(jmla);
//				mProgressHUD.dismiss();
//			} else {
//				mProgressHUD.dismiss();
//				(Toast.makeText(getApplicationContext(), "An error occured..", Toast.LENGTH_LONG)).show();
//			}
//			
//		}
//		@Override
//		public void onCancel(DialogInterface dialog) {
//			// TODO Auto-generated method stub
//			mProgressHUD.dismiss();
//		}
//
//	}
//	public class MockKMSMenu {
//		private String menuName;
//
//		public String getMenuName() {
//			return menuName;
//		}
//
//		public void setMenuName(String menuName) {
//			this.menuName = menuName;
//		}
//		
//	}
}
